const { title, content } = require("./mock");

module.exports = (app) => {
  app.get("/api/home/title", (req, res) => {
    res.send({
      code: 200,
      data: title,
    });
  });

  app.get("/api/detail/:id", (req, res) => {
    const { id } = req.params;

    res.send({
      code: 200,
      data: content.find((v) => v.id === id),
    });
  });

  app.get("/api/home/content", (req, res) => {
    const { id = 1, pageCode = 1, pageSize = 8 } = req.query;
    const list = content.filter((v) => +v.uid === +id);

    if (list.length >= pageCode * pageSize) {
      res.send({
        code: 200,
        data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
      });
    } else {
      res.status(400).send({
        code: 400,
        data: null,
        message: "没有更多数据了",
      });
    }
  });

  app.get("/api/detail/:id/zan", (req, res) => {
    const { id } = req.params;
    const item = content.find((v) => v.id === id);
    if (item.zan) {
      item.greter--;
    } else {
      item.greter++;
    }
    item.zan = !item.zan;

    res.send({
      code: 200,
      message: "OK",
    });
  });

  app.get("/api/list/search", (req, res) => {
    const { value } = req.query;
    const result = content.filter(
      (v) => v.title.includes(value) || v.desc.includes(value)
    );
		res.send({
			code: 200,
			data: result
		})
  });
};
